package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzPageRequest;
import com.itheima.pojo.PageResponseResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    //分页条件查询所有班级
    @Override
    public PageResponseResult<Clazz> queryPageClazz(ClazzPageRequest clazzPageRequest) {
//        1.给pageHelper传入参数,进行分页查询
        PageHelper.startPage(clazzPageRequest.getPage(),clazzPageRequest.getPageSize());

//        2.调用mapper层方法,获取查询结果
        List<Clazz> clazzList = clazzMapper.queryPageClazz(clazzPageRequest);

//        3.强制类型转换
        Page<Clazz> page =  (Page<Clazz>) clazzList;

//        4.封装查询结果
        return new PageResponseResult(page.getTotal(), page.getResult());
    }

    //新增班级
    @Override
    public void insertClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClazz(clazz);
    }

    //修改班级,1.根据id查询班级
    @Override
    public Clazz queryByIdClazz(Integer id) {
        return clazzMapper.queryByIdClazz(id);
    }

    //修改班级,2.修改信息
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    //删除班级
    @Override
    public void deleteClazz(Integer id) {
        clazzMapper.deleteClazz(id);
    }

    //查询全部班级
    @Override
    public List<Clazz> queryClazzs() {
        return clazzMapper.queryClazzs();
    }
}
